package es.ignapziriza.ccrypto.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import es.ignapziriza.ccrypto.ciphers.substitution.SubstitutionCipher;

public class SubstitutionCipherTesting {

	private SubstitutionCipher substCiphering;

	@Before
	public void setUp() throws Exception {
		substCiphering = new SubstitutionCipher();
	}

	@After
	public void tearDown() throws Exception {
		substCiphering = null;
	}

	@Test
	public void testCipherText1() {
		substCiphering.setKeyword("ZEBRAS");
		assertEquals("ZEBRASCDFGHIJKLMNOPQTUVWXY",
				substCiphering.getCipherAlphabet());
	}

	@Test
	public void testCipherText2() {
		substCiphering.setKeyword("ZEBRAS");
		substCiphering.cipher("flee at once. we are discovered!");
		assertEquals("SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!",
				substCiphering.getCipheredText());
	}

	@Test
	public void testFormatForTransmission1() {
		try {
			substCiphering.setKeyword("ZEBRAS");
			substCiphering.cipher("flee at once. we are discovered!");
			substCiphering.formatForTransmission(5);
			assertEquals("SIAAZ QLKBA VAZOA RFPBL UAOAR",
					substCiphering.getCipheredText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testFormatForTransmission2() {
		try {
			substCiphering.setKeyword("ZEBRAS");
			substCiphering.cipher("flee at once. we are discovered!");
			substCiphering.formatForTransmission(7);
		} catch (Exception e) {
			assertTrue("Esto sale si falla el test",
					e.getMessage().contains("char"));
		}
	}

	@Test
	public void testDecipherText1() {
		substCiphering.setKeyword("ZEBRAS");
		substCiphering.decipher("SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!");
		assertEquals("FLEE AT ONCE. WE ARE DISCOVERED!",
				substCiphering.getCipheredText());
	}

	@Test
	public void testFormatForTransmission3() {
		substCiphering.setKeyword("ZEBRAS");
		substCiphering.decipher("SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!");
		try {
			substCiphering.formatForTransmission(5);
			assertEquals("FLEEA TONCE WEARE DISCO VERED",
					substCiphering.getCipheredText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
